<?php
class List_katalog extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('perpus/refrensi_model');
	}
	
	function index($act='')
	{
		$data['value_rak'] = '';
		if($act == 'act_filter'){
			$data['value_rak'] = $this->input->post('id_rak');
			$this->session->set_userdata('sess_id_rak', $this->input->post('id_rak'));
		}else{
			$data['value_rak'] = $this->session->userdata('sess_id_rak');
		}
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		if($this->session->userdata('roleid')=='0'){ //diknas
			$colModel['NAMA_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		}
		$colModel['KODE_KATALOG'] = array('Kode Katalog',100,TRUE,'center',1);
		$colModel['NAMA_KATALOG'] = array('Nama Katalog',400,TRUE,'center',1);
		$colModel['KETERANGAN'] = array('Keterangan',400,TRUE,'center',1);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Katalog'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		if(!empty($data['value_rak']))
		$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("perpus/list_katalog/listkatalog_grid/".$data['value_rak']),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					location.href='".base_url()."index.php/perpus/list_katalog/input_katalog/'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/perpus/list_katalog/delete_katalog')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";	
		$data['filter_rak'] = $this->refrensi_model->get_list_rak();
		$data['url_filter'] = 'perpus/list_katalog/index/act_filter';
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listkatalog_grid($id_rak='')
	{
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		if($this->session->userdata('roleid')=='0') //diknas
			$valid_fields = array('NAMA_SEKOLAH','KODE_KATALOG','NAMA_KATALOG','KETERANGAN');
		else	
			$valid_fields = array('KODE_KATALOG','NAMA_KATALOG','KETERANGAN');
		
		$this->flexigrid->validate_post('ID_KATALOG','asc',$valid_fields);

		$records = $this->refrensi_model->get_katalog($id_rak);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			if($this->session->userdata('roleid')=='0'){ //diknas{
				$record_items[] = array($row->ID_KATALOG,
				$no+1,
				'<div align="center">'.$row->NAMA_SEKOLAH.'</div>',
				'<div align="center">'.$row->KODE_KATALOG.'</div>',
				'<div align="left">'.$row->NAMA_KATALOG.'</div>',
				'<div align="left">'.$row->KETERANGAN.'</div>',
				'<a href=\''.site_url().'/perpus/list_katalog/edit_katalog/'.$row->ID_KATALOG.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}else{
				$record_items[] = array($row->ID_KATALOG,
				$no+1,
				'<div align="center">'.$row->KODE_KATALOG.'</div>',
				'<div align="left">'.$row->NAMA_KATALOG.'</div>',
				'<div align="left">'.$row->KETERANGAN.'</div>',
				'<a href=\''.site_url().'/perpus/list_katalog/edit_katalog/'.$row->ID_KATALOG.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
		
	function cek_validasi() {	
			// Setting Rules
			if($this->session->userdata('roleid')=='0') //diknas
				$this->form_validation->set_rules('nama_sekolah', 'Nama Sekolah', 'required');
			
			$this->form_validation->set_rules('kode_katalog', 'Kode Katalog', 'required');
			$this->form_validation->set_rules('nama_katalog', 'Nama Katalog', 'required');
			$this->form_validation->set_rules('id_rak', 'Nama Rak', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function cek_katalog($kode){
		$rs_cek = $this->refrensi_model->validasi_id_katalog($kode)->row();
		return $rs_cek;
	}
	
	function input_katalog()
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['rak'] = $this->refrensi_model->get_list_rak();
			$data['content'] = $this->load->view('perpus/form_katalog',$data,true);
			$this->load->view('main',$data);
		}
		else{
			if($this->cek_katalog($this->input->post('kode_katalog'))->jml > 0){
				$data['status'] = 'new';
				$data['rak'] = $this->refrensi_model->get_list_rak();
				$data['value'] = $this->input->post();
				$data['err_msg'] = 'Data Katalog Pustaka tersebut telah ada dalam database.';
				$data['content'] = $this->load->view('perpus/form_katalog',$data,true);
				$this->load->view('main',$data);
			}else{
				$this->insert_katalog();
			}
		}            
    }
	
	function insert_katalog(){
		if($this->session->userdata('roleid')=='0'){ //diknas
			$nama_sekolah = $this->input->post('nama_sekolah');
		}else{
			$nama_sekolah = $this->session->userdata('unitid');
		}
		
		$data = array(
			'ID_SEKOLAH'			=> $nama_sekolah,
			'ID_RAK'				=> $this->input->post('id_rak'),
			'KODE_KATALOG'			=> $this->input->post('kode_katalog'),
			'NAMA_KATALOG'			=> $this->input->post('nama_katalog'),
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		
		$this->refrensi_model->insert_katalog($data);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('perpus/list_katalog'); 
	}
	
	function edit_katalog($id_katalog)
    {
 		if ($this->cek_validasi() == FALSE){
				$data['status'] = 'edit';
				$data['rs_katalog'] = $this->refrensi_model->get_katalogID($id_katalog);
				$data['rak'] = $this->refrensi_model->get_list_rak();
				$data['content'] = $this->load->view('perpus/form_katalog',$data,true);
				$this->load->view('main',$data);
			}
			else{
				if($this->cek_katalog($this->input->post('kode_katalog'))->jml > 0 and $this->cek_katalog($this->input->post('kode_katalog'))->ID_KATALOG != $id_katalog ){
					$data['status'] = 'edit';
					$data['rs_katalog'] = $this->refrensi_model->get_katalogID($id_katalog);
					$data['rak'] = $this->refrensi_model->get_list_rak();
					
					$data['value'] = $this->input->post();
					$data['err_msg'] = 'Data Katalog Pustaka telah ada dalam database.';
					$data['content'] = $this->load->view('perpus/form_katalog',$data,true);
					$this->load->view('main',$data);
				}else{
					$this->update_katalog($id_katalog);
				}
			}            
    }
	
	function update_katalog($id_katalog){
		if($this->session->userdata('roleid')=='0'){ //diknas
			$nama_sekolah = $this->input->post('nama_sekolah');
		}else{
			$nama_sekolah = $this->session->userdata('unitid');
		}
	
		$data = array(
			'ID_SEKOLAH'			=> $nama_sekolah,
			'ID_RAK'				=> $this->input->post('id_rak'),
			'KODE_KATALOG'			=> $this->input->post('kode_katalog'),
			'NAMA_KATALOG'			=> $this->input->post('nama_katalog'),
			'KETERANGAN'	 		=> $this->input->post('keterangan')
		);
		$this->refrensi_model->update_katalog($data,$id_katalog);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('perpus/list_katalog'); 
	}
	
	//Delete
	function delete_katalog()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->refrensi_model->delete_katalog($ta_id);
						
			
			$msg .= "Data Katalog Pustaka berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
    
}
?>
